package com.jianganming.factory.simple;

/**
 * 此简单工厂通过反射来创建产品，完美对解决了扩展问题，扩展新对产品，不需要修改工厂代码
 */
public class SimpleFactory {
    public static void main(String[] args) {
        Car car = CarFactroy.makeCar(BenchiCar.class,"奔驰","red");
        System.out.println(car);
        Car car1 = CarFactroy.makeCar(BaoMaCar.class,"宝马","yellow");
        System.out.println(car1);
        Car car2 = CarFactroy.makeCar(Car.class,"普通汽车","black");
        System.out.println(car2);
    }
}


class CarFactroy{

    public static Car makeCar(Class<? extends Car> clazz,String name,String color) {
        Car car = null;
        try {
            car = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            car = new Car();
        }
        car.setName(name);
        car.setColor(color);
       return car;
    }
}

class Car{
    private String name;
    private String color;
    public Car(){
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}

class BaoMaCar extends Car{

}

class BenchiCar extends Car{

}
